package edu.scau.divineplace.controller;

import edu.scau.divineplace.config.global.log.WebLog;
import edu.scau.divineplace.service.ImageService;
import edu.scau.divineplace.util.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:47
 */
@Slf4j
@RestController
@CrossOrigin
@Api("File控制器")
@WebLog(value = "File控制器")
@RequestMapping("/img")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @ApiOperation("上传图片")
    @WebLog(value = "上传图片", printArgs = false, printResult = false)
    @GetMapping("/{fileName}")
    public String get(@PathVariable String fileName) throws IOException {
        return imageService.get(fileName);
    }

    /**
     * 不能用@RequestBody修饰MultipartFile，修饰后图片无法上传
     * @param file
     * @throws IOException
     */
    @ApiOperation("上传图片")
    @PostMapping("")
    public String add(MultipartFile file) throws IOException {
        return imageService.add(file);
    }

}
